﻿@using MvcContrib.UI.Grid
@model XRM.Security.WebUI.Models.InfoSystem.InfoSystemModel
@{
    ViewBag.Title = String.Format("{0}", Model.IsEdit ? Model.InfoSystemName : "Добавление АИС");
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("ReturnLinkPartial", new Tuple<string, string>(Url.Action("Index"), "Вернуться к списку информационных систем"))

<div>
    @using (Html.BeginForm("Edit", "InfoSystem", FormMethod.Post, new { @class = "form-horizontal form-horizontal-2" }))
    {
        @Html.AntiForgeryToken()

        const string inputCss = "form-control";
        const string divCss = "col-sm-4";

        @Html.HiddenFor(m => m.Id)
        @Html.HiddenFor(m => m.DateIn)
        
        if (Model.IsEdit)
        {
            <div class="form-group">
                @Html.LabelFor(m => m.DateIn, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBox("DateIn", Model.DateIn.ToString("dd.MM.yyyy"), new { @class = @inputCss, disabled = "disabled" })
                </div>
            </div>
        }

            <div class="form-group">
                @Html.LabelFor(m => m.InfoSystemName, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.InfoSystemName, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.InfoSystemName)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.DomainGroup, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.DomainGroup, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.DomainGroup)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Operator, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.Operator, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.Operator)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.OperatorContact, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.OperatorContact, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.OperatorContact)
                </div>
                @if(!Model.ReadOnly)
                {
                    <div class="control-label-left">
                        <i class="fa fa-question-circle fa-action" rel="tooltip" title="ФИО, email, телефон и другие контакты сотрудника оператора"></i>
                    </div>
                }
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Developer, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.Developer, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.Developer)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Support, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.Support, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.Support)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(m => m.Comment, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextAreaFor(m => m.Comment, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.Comment)
                </div>
            </div>

            if (Model.IsEdit)
            {
                <div class="form-group">
                    @Html.LabelFor(m => m.InMaintenance, new { @class = "control-label required" })
                    <div class="@divCss">
                        @Html.DropDownListFor(m => m.InMaintenance, new SelectList(Model.StatusList, "Value", "Text"), new { @class = @inputCss })
                    </div>
                </div>
        
                <div class="form-group" id="divDateOut">
                    @Html.LabelFor(m => m.DateOut, new { @class = "control-label" })
                    <div class="@divCss">
                        @Html.TextBox("DateOut", Model.DateOut.Value.ToString("dd.MM.yyyy"), new { @class = @inputCss, disabled = "disabled" })
                    </div>
                </div>
            }
            if (!Model.ReadOnly)
            {
                <div class="form-group">
                    <label class="control-label">Паспорт или положение о системе</label>
                    <div class="@divCss">
                        @Html.Partial("_FileUploaderPartial", new ViewDataDictionary { { "UploadUrl", Model.FileUploadUrl } })
                    </div>
                </div>
            }

            if (Model.IsEdit)
            {
                if (Model.FileList.Any())
                {
                Html.Grid(Model.FileList).Columns(column =>
                    {
                        column.For(item => Html.ActionLink(item.FileName, "DownloadInfoSystemFile", "InfoSystem", new { id = item.Id }, null)).Named("Паспорт или положение о системе");
                        column.For(item => item.UploadDate.ToString("dd.MM.yyyy")).Named("Дата загрузки");

                        if (!Model.ReadOnly)
                        {
                            column.Custom(@<text>@Html.Partial("_GridActionsPartial", new ViewDataDictionary { { "Controller", "InfoSystem" }, { "Id", item.Id }, {"Action", "DeleteFile"} })</text>)
                                .Named("")
                                .Attributes(width => "40px");
                        }
                    }).Attributes(@class => "table table-striped table-bordered", id => "table_id")
                    .Empty("Нет файлов")
                    .Render();
                }
            }
            
            if(!Model.ReadOnly)
            {
                @Html.Partial("_FormActionsPartial")
            }
    }
</div>



@section scripts
{
     @Scripts.Render("~/bundles/gridlibrary")

    <script type="text/javascript" src="@(Url.Content("~/Scripts/FileUploader.js"))"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        @if (Model.IsEdit)
        {
            if (Model.FileList.Any())
            {
            <text>
            $('#table_id').dataTable({
                paging: false,
                info: false,
                bFilter: false,
                bSort: false
            });
            </text>
            }

            if (Model.InMaintenance)
            {
                <text>
                $('#divDateOut').hide();
                $('#InMaintenance').change(function () {
                    if ($('#InMaintenance').val() == "True") {
                        $('#divDateOut').hide();
                    }
                    else {
                        $('#divDateOut').show();
                    }
                });
                </text>
            }
            else
            {
                <text>$("#InMaintenance").prop('disabled', true);</text>
            }
        }

        @if (Model.ReadOnly)
        {
            <text>ReadOnlyView();</text>
        }
    });
    </script>
}